import 'package:flutter/material.dart';
import 'package:studymanage/util/jpush_manager.dart';
import 'package:studymanage/util/request.dart';


class Message extends StatefulWidget {
  Message({Key key, this.title}) : super(key: key);

  final String title;

  @override
  State<StatefulWidget> createState() {
    return MessageState();
  }
}

class MessageState extends State<Message> {

  List messages = new List();
  bool isLoading = false;
  int _page=1;

  @override
  void initState() {
    _getData();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          elevation: 0,
          backgroundColor: Colors.white,
          title: Text("消息",style: TextStyle(color: Color.fromRGBO(51, 51, 51, 1)),),
          centerTitle: false,
          leading:null,
        ),
        body: Container(
          height: MediaQuery.of(context).size.height,
          child: RefreshIndicator(
            onRefresh: ()=>_refresh(),
            child: messages.length > 0 ? ListView.builder(
                itemCount: messages.length,
                padding: EdgeInsets.only(bottom: 85),
                itemBuilder: (c,i){
                  return Container(
                    height: 50,
                    margin: EdgeInsets.only(right: 20,left: 20),
                    decoration: BoxDecoration(
                        border: Border(
                            bottom: BorderSide(color: Color(0xffEEEEEE))
                        )
                    ),
                    child: Row(
                      children: [
                        Container(
                            margin: EdgeInsets.only(right: 10),
                            child: Image.asset("assets/images/login/kc_xxjh.png")
                        ),
                        Text(messages[i]['content'],style: TextStyle(color: Color(0xff333333),fontSize: 14),)
                      ],
                    ),
                  );
                }
            ): Center(
              child: Text("暂无"),
            ),
          ),
        )
    );
  }

  @override
  void dispose() {
    super.dispose();
  }

  _getData(){
    if(isLoading){
      return;
    }
    setState(() {
      isLoading = true;
    });
    Request().post("/multiapi/c221p_message",data: {
    "pageNum": _page,
    "pageSize": 10,
    }).then((response) {
      messages.addAll(response.data['list']);
      setState(() {
        if (!response.data["isLastPage"]) {
          isLoading = false;
          _page += 1;
        }
      });
    });
  }

  Future<Null> _refresh() async{
    JpushManager.instance.jpush.clearAllNotifications();
    JpushManager.instance.jpush.setBadge(0);
    setState(() {
      _page = 1;
      isLoading =false;
      messages = [];
    });
    _getData();
    return null;
  }
}
